﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WP7Guide
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    public class ViewModelBase : INotifyPropertyChanged
    {
        private Dispatcher _uiDispatcher;
        private Dispatcher UIDispatcher
        {
            get
            {
                if (_uiDispatcher == null)
                {
                    _uiDispatcher = Deployment.Current.Dispatcher;
                }
                return _uiDispatcher;
            }
        }

        protected void InvokeUI(Action action)
        {
            if (UIDispatcher.CheckAccess())
            {
                action();
            }
            else
            {
                UIDispatcher.BeginInvoke(action);
            }
        }

        private bool _isDataLoaded;
        public bool IsDataLoaded
        {
            get
            {
                return _isDataLoaded;
            }
            set
            {
                _isDataLoaded = value;
                NotifyPropertyChanged("IsDataLoaded");
            }
        }
        
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                InvokeUI(() => handler(this, new PropertyChangedEventArgs(propertyName)));
            }
        }
    }
}